/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javatest1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.geom.*;

/**
 *
 * @author paul
 */
public class RoboWindow extends Canvas
{
     Graphics bufferGraphics;
     // The image that will contain everything that has been drawn on
     // bufferGraphics.
     Image offscreen;
     // To get the width and height of the applet.
     Dimension dim;

    class Mouse extends MouseInputAdapter {
      public void mouseMoved(MouseEvent e) {
        // System.out.printf("mouse moved: %d\n", e.getX());
      }

      public void mouseDragged(MouseEvent e) {
      }

      public void mouseClicked(MouseEvent e) {
      }
      public void mousePressed(MouseEvent e){
          if(e.getButton() == e.BUTTON1) {
              robot.lineTracker.left = true;
          }
          if(e.getButton() == e.BUTTON2) {
              robot.lineTracker.center = true;
          }
          if(e.getButton() == e.BUTTON3) {
              robot.lineTracker.right = true;
          }
      }
      public void mouseReleased(MouseEvent e) {
          System.out.printf("mouse released: %d\n", e.getX());
          if(e.getButton() == e.BUTTON1) {
              robot.lineTracker.left = false;
          }
          if(e.getButton() == e.BUTTON2) {
              robot.lineTracker.center = false;
          }
          if(e.getButton() == e.BUTTON3) {
              robot.lineTracker.right = false;
          }
      }
    }

    public class Keyboard implements KeyListener {
        /** Handle the key typed event from the text field. */
        public void keyTyped(KeyEvent e) {
            char c = e.getKeyChar();
            int k = e.getKeyCode();
            System.out.printf("Key char:'%c', code:%d\n", c, k);
            switch(c){
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                    robot.manipulator.goToPeg(c - '0');
                    break;
                case 'a':
                    robot.autoPilot.start();
                    break;
                case 'x':
                    robot.autoPilot.stop();
                    robot.drive.go(0, 0, 0);
                    break;
                case 'i':
                    robot.lineTracker.straightLine = true;
                    robot.lineTracker.splitLine = false;
                    break;
                case 'y':
                    robot.lineTracker.straightLine = false;
                    robot.lineTracker.splitLine = true;
                    break;
                case 'r':
                    robot.drive.x = 0;    // behind tracking line
                    robot.drive.y = -24;    // behind tracking line
                    robot.drive.heading = 2;   // slightly off
                    break;
                case '<':
                    break;
                case '>':
                    break;
            }
        }

        /** Handle the key-pressed event from the text field. */
        public void keyPressed(KeyEvent e) {
            int k = e.getKeyCode();
            System.out.printf("Key code:%d\n", k);
            switch(k){
                case 38:
                    robot.drive.go(1, 0, 0);
                    break;
                case 40:
                    robot.drive.go(-1, 0, 0);
                    break;
                case 37:
                    robot.drive.go(0, 0, -1);
                    break;
                case 39:
                    robot.drive.go(0, 0, 1);
                    break;
                default:
                    break;
            }
        }

        /** Handle the key-released event from the text field. */
        public void keyReleased(KeyEvent e) {
            int k = e.getKeyCode();
            System.out.printf("Key code:%d\n", k);
            switch(k){
                case 38:
                    robot.drive.go(0, 0, 0);
                    break;
                case 40:
                    robot.drive.go(0, 0, 0);
                    break;
                case 37:
                    robot.drive.go(0, 0, 0);
                    break;
                case 39:
                    robot.drive.go(0, 0, 0);
                    break;
                default:
                    break;
            }
        }
    }
    RoboRobot robot = null;
    double armHeightScale = 0.4;
    Mouse mouse = new Mouse();
    Keyboard keyboard = new Keyboard();

    public RoboWindow()
    {
        this.addMouseListener(mouse);
        this.addMouseMotionListener(mouse);
        this.addKeyListener(keyboard);
    }

    void init(){
        // double buffering setup
        dim = getSize();
        // We'll redraw the applet eacht time the mouse has moved.
        setBackground(Color.white);
        // Create an offscreen image to draw on
        // Make it the size of the applet, this is just perfect larger
        // size could slow it down unnecessary.
        offscreen = createImage(dim.width,dim.height);
        // by doing this everything that is drawn by bufferGraphics
        // will be written on the offscreen image.
        bufferGraphics = offscreen.getGraphics();

    }
    public void paint(Graphics og)
    {
        Graphics g = bufferGraphics;

        //bufferGraphics.clearRect(0,0,dim.width,dim.width);
    
        // super.paintAll(g);
        /* We override the method here. The graphics
         * code comes here within the method body. */
        // draw entire component white
        g.setColor(Color.white);
        g.fillRect(0, 0, getWidth(), getHeight());

        // yellow circle
        // g.setColor(Color.yellow);
        // g.fillOval(0, 0, fieldLength, fieldLength);

        // magenta circle
        // g.setColor(Color.magenta);
        // g.fillOval(160, 160, fieldLength, fieldLength);


        // rx, ry - top-left corner of the drive base
        int rx = 20;
        int ry = 400;

        // draw robot name & time
        g.setColor(Color.red);
        g.drawString("3630", 400, 20);
        String s = new String();
        s = "T=" + (int) (robot.autoPilot.taskTime*1000) + "ms\"";
        g.drawString(s, 400, 40);

        // robot is 200x200, another 200 up for the arm
        if(robot != null){
            g.setColor(Color.blue);
            g.fillRect(rx, ry, 200, 40);
            g.fillRect(rx + 100, ry-200, 10, 200);

            // elevator
            g.setColor(Color.green);
            int ey = (int)(ry - (robot.manipulator.currentHeight - robot.manipulator.baseHeight) / (robot.manipulator.maxHeight - robot.manipulator.baseHeight) * 200);
            g.fillRect(rx + 110, ey-10, 10, 10);

            // arm
            int ax1 = rx + 110 + 10;
            int ay1 = ey - 5;

            Graphics2D g2d = (Graphics2D)g;
            // save the original transform state
            AffineTransform saveAT = g2d.getTransform();

            g.setColor(Color.red);
            
            g2d.translate(ax1, ay1);
            g2d.rotate(Math.toRadians(robot.manipulator.currentAngle));
            g2d.fillRect(0, -180, 5, 180);
            g2d.fillRect(0, -140, 30, 5);
            g2d.fillRect(25, -180, 5, 40);

            // restore transform
            g2d.setTransform(saveAT);

            int ax2 = ax1 + (int)(200 * Math.sin(Math.toRadians(robot.manipulator.currentAngle)));
            int ay2 = ay1 - (int)(200 * Math.cos(Math.toRadians(robot.manipulator.currentAngle)));

            // now the moving robot on the field - the field (100" wide, 300"long)
            // tracking lane is about 200" long, Y-split is about 84" from the end if present

            int fx1 = 550;
            int fy1 = 600;
            double robotScale = 700/300;
            int robotWidth = (int)robot.drive.width;    // in
            int robotHeight = (int)robot.drive.length;   // in

            // we set the transform so coordinates are in inches, y increasing up
            g2d.translate(fx1, fy1);
            g2d.scale(robotScale, -robotScale);
            // g2d.rotate(Math.toRadians(180));

            // draw a field around the origin (robot starting point

            // line marking
            g.setColor(Color.gray);

            int fieldLength = 220;
            // strainght line
            if(robot.lineTracker.straightLine){
                g2d.fillRect(-1, 0, 2, fieldLength);
                g2d.fillRect(-6, fieldLength - 2, 12, 2);
            }

            // Y line
            if(robot.lineTracker.splitLine){
                int dx = 12 * 3;
                int dy = 12 * 6;
                g2d.fillRect(-1, 0, 2, fieldLength-dy);

                g2d.fillRect(-dx-1, fieldLength-12, 2, 12); // vertical stub
                g2d.drawLine(0, fieldLength - dy, -dx, fieldLength-12); // diagonal
                g2d.fillRect(-dx-6, fieldLength-2, 12, 2);    // cross

                g2d.fillRect(dx-1, fieldLength-12, 2, 12);  // vertical stub
                g2d.drawLine(0, fieldLength - dy,  dx, fieldLength-12);   // diagonal
                g2d.fillRect(dx-6, fieldLength-2, 12, 2);     // cross
            }

            g.setColor(Color.yellow);
            g2d.drawOval(-10, -10, 20, 20);
            g2d.drawRect(-100, -50, 200, 300);   // 200x300" field
            g2d.drawRect(-robotWidth / 2, -robotHeight, robotWidth, robotHeight);

            //g2d.translate(dx, dy);
            //g2d.scale(robotScale, robotScale);
            g2d.translate(robot.drive.x, robot.drive.y);
            g2d.rotate(Math.toRadians(-robot.drive.heading));

            // draw line tracker
            g.setColor(robot.lineTracker.left?Color.RED:Color.green);
            g2d.fillRect(-7, robotHeight/2 - 5, 5, 5);
            g.setColor(robot.lineTracker.center?Color.RED:Color.green);
            g2d.fillRect(-2, robotHeight/2 - 5, 5, 5);
            g.setColor(robot.lineTracker.right?Color.RED:Color.green);
            g2d.fillRect(2, robotHeight/2 - 5, 5, 5);

            // draw line body
            g.setColor(Color.BLACK);
            g2d.draw3DRect(-robotWidth / 2, -robotHeight/2, robotWidth, robotHeight, true);

            // restore transform
            g2d.setTransform(saveAT);
        }
        og.drawImage(offscreen,0,0,this);
    }
public void update(Graphics g)
     {
          paint(g);
     } 
}
